//
//  JCWBFInputSMSCodeAlertView.swift
//  HengYouCai
//
//  Created by wll on 2018/1/25.
//  Copyright © 2018年 lwj. All rights reserved.
//  宝付支付输入验证码弹框视图

import UIKit

class JCWBFInputSMSCodeAlertView: UIView,UITextFieldDelegate {
    deinit {
        debugPrint("JCWBFInputSMSCodeAlertView dealloc")
    }
    
    private var phoneNumber: String = "****"
    private var sureHandler: ((_ smsCode: String)->Void)?
    private var num = -1
    private var smsCode = ""
    //MARK:构造函数
    
    ///弹框  title: 标题  message: 内容  确认回调返回验证码
    @discardableResult convenience init(phoneNumber: String?, num: Int, sureHandler: ((_ smsCode: String)->Void)?){
        self.init(frame: UIScreen.main.bounds)
        self.phoneNumber = phoneNumber ?? "****"
        self.sureHandler = sureHandler
        assert(num > 0, "JCWBFInputSMSCodeAlertView init fail, because num <= 0")
        self.num = num
        self.createUI()
    }
    
    //MARK:添加UI
    private func createUI() {
        
        self.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        UIApplication.shared.keyWindow?.addSubview(self)


        self.addSubview(self.cv)
        
        self.cv.addSubview(self.titleLab)
        
        self.cv.addSubview(self.sepLine)
        
        self.cv.addSubview(self.messageLab)
        
        self.cv.addSubview(self.tF)
        weak var weakSelf = self
        self.tF.inputCompletionHandler = { (str) in
            weakSelf?.smsCode = str
        }
        
        self.cv.addSubview(self.bottomBtn)
        
        self.cv.addSubview(self.closeBtn)
        
        self.wl_animationShow(view: self.cv)

    }
    ///弹簧动画
    func wl_animationShow(view: UIView) {

        view.center = CGPoint(x: self.centerX_sd, y: -view.height_sd)
        UIView.animate(withDuration: 1.2, delay: 0.0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.6, options: .curveLinear, animations: {
            view.center = CGPoint(x: self.centerX_sd, y: self.centerY_sd-50)
            
        }) { (finished) in
            //唤起键盘
            self.tF.becomeFirstResponder()
        }
    }
    
    //MARK:布局
    override func layoutSubviews() {
        super.layoutSubviews()
        let witdth = self.frame.size.width - 70
        var height: CGFloat = 0
        
        self.titleLab.frame = CGRect(x: 0, y: 0, width: witdth, height: 45)
            height += 45
        
        self.closeBtn.frame = CGRect(x: witdth-45, y: 0, width: 45, height: 45)
        

        height += 0.5
        
        self.sepLine.frame = CGRect(x: 0, y: height, width: witdth, height: 0.5)

        
        height += 23//margin
        let labH = self.messageLab.frame.size.height + 0.1
        self.messageLab.frame = CGRect(x: 25, y: height, width: witdth-50, height: labH)
        height += labH
        height += 23//margin
        
        self.tF.frame = CGRect(x: 25, y: height, width: witdth-50, height: 40)
        height += 40
        height += 16//margin
        
        self.bottomBtn.frame = CGRect(x: 25, y: height, width: witdth-50, height: 36)
        
        height += 36
        height += 22.5//margin
        
        self.cv.bounds = CGRect(x: 0, y: 0, width: witdth, height: height)
        self.cv.center = self.center
        self.cv.centerY_sd -= 50
    }
    
    
    //MARK:UI
    ///中间白框
    lazy var cv: UIView = {
        let temp = UIView()
        temp.backgroundColor = UIColor.white
        temp.layer.cornerRadius = 10
        return temp
    }()
    ///华丽的分割线
    lazy var sepLine:UIView = {
        let temp = UIView()
        temp.backgroundColor = Normal_CCCColor
        return temp
    }()
    ///右上角关闭按钮
    lazy var closeBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.setImage(UIImage(named: "calcu_close"), for: .normal)
        temp.addTarget(self, action: #selector(closeBtnClick), for: .touchUpInside)
        return temp
    }()
    ///标题
    lazy var titleLab: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .center
        temp.font = Font_WL(15)
        temp.text = "安全验证"
        temp.textColor = Normal_333Color
        return temp
    }()
    ///内容
    lazy var messageLab: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .center
        let text: NSMutableAttributedString = NSMutableAttributedString(string: "为保证您的资金安全\n已向", attributes: [NSFontAttributeName: Font_WL(12),NSForegroundColorAttributeName: Normal_333Color])
        text.append(NSAttributedString(string: self.phoneNumber, attributes: [NSFontAttributeName: Font_WL(14),NSForegroundColorAttributeName: Normal_RedColor]))
        text.append(NSAttributedString(string: "发送短信验证码", attributes: [NSFontAttributeName: Font_WL(12),NSForegroundColorAttributeName: Normal_333Color]))
        temp.attributedText = text
        temp.numberOfLines = 0
        temp.sizeToFit()
        return temp
    }()
    
    ///输入框
//    lazy var tF: UITextField = {
//        let temp = UITextField()
//        temp.keyboardType = .numberPad
//        temp.placeholder = "请输入短信验证码"
//        temp.font = Font_WL(13)
//        temp.layer.borderColor = Normal_999Color.cgColor
//        temp.layer.borderWidth = 0.5
//        temp.delegate = self
//        temp.textAlignment = .center
//        return temp
//    }()
    lazy var tF: WLSMSCodeInputView = {
        let temp = WLSMSCodeInputView(self.num)
//        temp.keyboardType = .numberPad
//        temp.placeholder = "请输入短信验证码"
//        temp.font = Font_WL(13)
//        temp.layer.borderColor = Normal_999Color.cgColor
//        temp.layer.borderWidth = 0.5
//        temp.delegate = self
//        temp.textAlignment = .center
        return temp
    }()
    
    ///按钮底部
    lazy var bottomBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.backgroundColor = Normal_RedColor
        temp.titleLabel?.textColor = UIColor.white
        temp.titleLabel?.font = Font_WL(14)
        temp.layer.cornerRadius = 5
        temp.setTitle("确定", for: .normal)
        temp.addTarget(self, action: #selector(bottomBtnClick), for: .touchUpInside)
        return temp
    }()

    
    
    //MARK:响应事件
    func bottomBtnClick(sender: UIButton) {
        //不为空 长度在3-6位置之间
        if self.smsCode.count != self.num {
            self.showHUDFailTips("请输入正确的验证码", hideDelay: kHUDHideDelay)
            return
        }
        self.sureHandler?(self.smsCode)
        self.removeFromSuperview()
    }
    
    func closeBtnClick(sender: UIButton) {
        self.removeFromSuperview()
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        return range.location < 6
        
        
    }
}
